/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PaginaInicial.java
 *
 * Created on 08/07/2009, 10:36:42
 */
package interfaceGraficaUsuario;

import controle.Sistema;
import controle.UsuariosEnum;
import java.awt.Color;

/**
 *
 * @author João Paulo
 */
public class PaginaInicial extends javax.swing.JFrame {

    static UsuariosEnum tipoUsuario;
    private static PaginaInicial telaLogin = new PaginaInicial();

    /** Creates new form PaginaInicial */
    public PaginaInicial() {
        initComponents();
        dadosInvalidos.setForeground(Color.RED);
        labelRecSenha.setVisible(false);
        labelRecSenha.setForeground(Color.RED);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        campoLogin = new javax.swing.JTextField();
        campoSenha = new javax.swing.JPasswordField();
        labelLogin = new javax.swing.JLabel();
        labelSenha = new javax.swing.JLabel();
        botaoEntrar = new javax.swing.JButton();
        botaoRecuperarSenha = new javax.swing.JButton();
        logoCCC = new javax.swing.JLabel();
        dadosInvalidos = new javax.swing.JLabel();
        labelRecSenha = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("APOIO-LP2");
        setBackground(new java.awt.Color(255, 255, 255));
        setMinimumSize(new java.awt.Dimension(800, 600));

        labelLogin.setFont(new java.awt.Font("Tahoma", 0, 14));
        labelLogin.setText("Login:");

        labelSenha.setFont(new java.awt.Font("Tahoma", 0, 14));
        labelSenha.setText("Senha:");

        botaoEntrar.setText("Entrar");
        botaoEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoEntrarActionPerformed(evt);
            }
        });
        botaoEntrar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                botaoEntrarKeyTyped(evt);
            }
        });

        botaoRecuperarSenha.setText("Recuperar Senha");
        botaoRecuperarSenha.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                botaoRecuperarSenhaMouseClicked(evt);
            }
        });
        botaoRecuperarSenha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoRecuperarSenhaActionPerformed(evt);
            }
        });
        botaoRecuperarSenha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                botaoRecuperarSenhaKeyTyped(evt);
            }
        });

        logoCCC.setIcon(new javax.swing.ImageIcon(getClass().getResource("/interfaceGraficaUsuario/ccc-full.jpg"))); // NOI18N

        dadosInvalidos.setForeground(new java.awt.Color(255, 0, 0));

        labelRecSenha.setText("A senha foi enviada para o seu email cadastrado");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(362, 362, 362)
                        .addComponent(botaoEntrar))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(340, 340, 340)
                        .addComponent(botaoRecuperarSenha))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(256, 256, 256)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(labelSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(labelLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(campoLogin, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                                    .addComponent(campoSenha, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                                    .addComponent(logoCCC, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(dadosInvalidos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addComponent(labelRecSenha, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))))
                .addContainerGap(255, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(131, Short.MAX_VALUE)
                .addComponent(logoCCC, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(dadosInvalidos, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(labelLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(campoLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(labelSenha))
                    .addComponent(campoSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(botaoEntrar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(botaoRecuperarSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(56, 56, 56)
                .addComponent(labelRecSenha)
                .addGap(41, 41, 41))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoRecuperarSenhaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botaoRecuperarSenhaMouseClicked
        labelRecSenha.setVisible(true);
        
    }//GEN-LAST:event_botaoRecuperarSenhaMouseClicked

    private void botaoEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoEntrarActionPerformed
        String login = campoLogin.getText();
        String senha = campoSenha.getText();
        defineUsuario(login, senha);


    }//GEN-LAST:event_botaoEntrarActionPerformed

    private void botaoEntrarKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_botaoEntrarKeyTyped
        String login = campoLogin.getText();
        String senha = campoSenha.getText();
        defineUsuario(login, senha);
    }//GEN-LAST:event_botaoEntrarKeyTyped

    private void botaoRecuperarSenhaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_botaoRecuperarSenhaKeyTyped
        labelRecSenha.setVisible(true);
    }//GEN-LAST:event_botaoRecuperarSenhaKeyTyped

    private void botaoRecuperarSenhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoRecuperarSenhaActionPerformed
        
        Sistema.enviaSenhaPorEmail(campoLogin.getText());
        
    }//GEN-LAST:event_botaoRecuperarSenhaActionPerformed

    private void defineUsuario(String login, String senha) {

        tipoUsuario = Sistema.confereLoginSenha(login, senha);
        if (tipoUsuario != UsuariosEnum.INEXISTENTE) {
            if (tipoUsuario == UsuariosEnum.ALUNO) {
                MenuAluno menuAluno = new MenuAluno(Sistema.dadosUsuario(login));
                menuAluno.setVisible(true);
                dispose();
            } else if (tipoUsuario == UsuariosEnum.MONITOR) {
                MenuMonitor menuMonitor = new MenuMonitor(Sistema.dadosUsuario(login));
                menuMonitor.setVisible(true);
                dispose();
            } else {
                MenuProfessor menuProf = new MenuProfessor(Sistema.dadosUsuario(login));
                menuProf.setVisible(true);
                dispose();


            }
        } else {
            dadosInvalidos.setText("Login e/ou Senha inválidos.");
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                telaLogin.setVisible(true);
                Sistema.inicia();
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoEntrar;
    private javax.swing.JButton botaoRecuperarSenha;
    private javax.swing.JTextField campoLogin;
    private javax.swing.JPasswordField campoSenha;
    private javax.swing.JLabel dadosInvalidos;
    private javax.swing.JLabel labelLogin;
    private javax.swing.JLabel labelRecSenha;
    private javax.swing.JLabel labelSenha;
    private javax.swing.JLabel logoCCC;
    // End of variables declaration//GEN-END:variables
}
